//
//  THFlowEarnMypublishCell.m
//  YJB
//
//  Created by 中电大维 on 2017/1/13.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THFlowEarnMypublishCell.h"
#import "THFlowEarnMypublishModel.h"
#import "THFlowEarnAutoMyPubModel.h"
NSString *const flowEarnMypublishCellID = @"flowEarnMypublishCellID";
@implementation THFlowEarnMypublishCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //self.backgroundColor = kUIColorFromRGB(0xffebe7);
        //self.backgroundColor = [UIColor colorWithRed:rand()%255/255.0 green:rand()%255/255.0 blue:rand()%255/255.0 alpha:1];
        [self cpcTest];
    }
    return self;
}

- (NSMutableAttributedString *)getAttributedString:(NSString *)front Middle:(NSString *)middle Behind:(NSString *)behind{

    NSMutableAttributedString *attr = [NSString attributedString:front frontColor:kUIColorFromRGB(0x666666) middle:middle middleColor:kUIColorFromRGB(0xff783c) behind:behind behindColor:kUIColorFromRGB(0x666666) font:FONT(14)];
    return attr;
}

- (void)cpcTest
{
    self.iconImgView.image = Image(@"th_cpc_default_normal");
    self.titleLab.text = @"只要轻松点几下，钱就到手啦，亲还在等什么呢，赶紧点我吧...";
    
    [self.vipAwardBtn setBackgroundColor:[UIColor whiteColor]];
    //    self.award.text = @"普通奖励 3金币";
    //[self.vipAwardBtn setTitle:@"奖励" forState:UIControlStateNormal];
    
//    self.vipAward.text = @"18 金币";
//    self.surplus.text = @"已做过8/10个";
    
    self.award.attributedText = [self getAttributedString:@"已做" Middle:@"8" Behind:@"/16 个"];
    //    self.vipAwardBtn.titleLabel.attributedText = [self getAttributedString:@"奖励" Middle:@" " Behind:@"18金币"];
    
    //self.vipAwardBtn.attributedText = [self getAttributedString:@"普通奖励" Middle:@" " Behind:@"16金币"];
    
    //    self.vipAwardBtn.backgroundColor = [UIColor orangeColor];
    //    self.vipAwardBtn.imageView.backgroundColor = [UIColor yellowColor];
    //    self.vipAwardBtn.titleLabel.backgroundColor = [UIColor redColor];
    
    
}

-(UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.image = Image(@"th_cpc_default_normal");
        [self addSubview:_iconImgView];
        [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(10);
            make.left.equalTo(self.mas_left).offset(15);
            make.width.height.mas_equalTo(48);
        }];
    }
    return _iconImgView;
}

-(UILabel *)titleLab
{
    if (_titleLab == nil)
    {
        _titleLab = [[UILabel alloc]init];
        _titleLab.backgroundColor = [UIColor clearColor];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.numberOfLines = 0;
        _titleLab.textColor = kUIColorFromRGB(0x333333);
        [self addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(16);
            make.left.equalTo(self.iconImgView.mas_right).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(32);
        }];
    }
    return _titleLab;
}

-(UILabel *)award
{
    if (_award == nil)
    {
        _award = [[UILabel alloc]init];
        _award.backgroundColor = [UIColor clearColor];
        _award.font = [UIFont boldSystemFontOfSize:14];
        _award.numberOfLines = 0;
        _award.textColor = kUIColorFromRGB(0x666666);
        [self addSubview:_award];
        NSString *award = [NSString stringWithFormat:@"普通奖励 3金币"];
        CGFloat awardWidth = [NSString widthForString:award UIfontSize:FONT(15) andHeight:15];
        [_award mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLab.mas_bottom).mas_offset(15);
            make.left.equalTo(self.titleLab.mas_left);
            make.width.mas_equalTo(awardWidth);
            make.height.mas_equalTo(15);
        }];
    }
    
    
    return _award;
}

//-(UILabel *)surplus
//{
//    if (_surplus == nil)
//    {
//        _surplus = [[UILabel alloc]init];
//        _surplus.font = FONT(14);
//        _surplus.textColor = kUIColorFromRGB(0x666666);
//        [self addSubview:_surplus];
//        [_surplus mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.titleLab.mas_left);
//            make.height.mas_equalTo(15);
//            make.top.equalTo(self.titleLab.mas_bottom).offset(15);
//        }];
//    }
//    return _surplus;
//}

- (void)detailItem:(UIButton *)button
{
    [self.delegate detailItem:button];
}

- (UIButton *)vipAwardBtn
{
    if (!_vipAwardBtn) {
        _vipAwardBtn = [[UIButton alloc] init];
        [_vipAwardBtn setTitleColor:kUIColorFromRGB(0xff783c) forState:UIControlStateNormal];
        _vipAwardBtn.titleLabel.font = FONT(15);
        _vipAwardBtn.adjustsImageWhenHighlighted = NO;
        [_vipAwardBtn setTitle:@"查看完成详情 >>" forState:UIControlStateNormal];
        [_vipAwardBtn addTarget:self action:@selector(detailItem:) forControlEvents:UIControlEventTouchUpInside];
        _vipAwardBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self addSubview:_vipAwardBtn];
        [_vipAwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.award.mas_centerY);
            make.left.equalTo(self.award.mas_right).offset(5);
            make.right.equalTo(self.mas_right).offset(-15);
            make.width.equalTo(self.award.mas_width);
            make.height.mas_equalTo(15);
        }];
    }
    return _vipAwardBtn;
}
-(void)setAutoModel:(THFlowEarnAutoMyPubModel *)model
{
    self.titleLab.text = model.sitename;
    self.award.attributedText = [self getAttributedString:@"已做" Middle:[NSString stringWithFormat:@"%d",[model.surfedcount intValue]] Behind:[NSString stringWithFormat:@"/%d个",[model.surflimit intValue]]];
    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.titleHeight);
    }];
}

-(void)setModel:(THFlowEarnMypublishModel *)model
{
    self.titleLab.text = model.sitename;
    self.award.attributedText = [self getAttributedString:@"已做" Middle:[NSString stringWithFormat:@"%d",[model.surfedcount intValue]] Behind:[NSString stringWithFormat:@"/%d个",[model.surflimit intValue]]];
    NSLog(@"%@",[NSString stringWithFormat:@"%@",model.surf_icon]);
    
    
    NSString *urlString = model.surf_icon;
    if (![urlString hasPrefix:@"http"])
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];

    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.titleHeight);
    }];
}


@end
